package com.edu.exam.entity.assessment.plan;

import com.edu.exam.enums.TemplateType;
import com.edu.exam.enums.TemplateStatus;
import com.edu.exam.enums.exam.ExamType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 考试计划模板实体类
 * 所属模块：B-05 考核计划管理模块
 * 对应数据库表：exam_plan_template
 *
 * <p>考试计划模板实体，定义考试计划的标准格式和配置。
 * 模板包含计划的基本结构、字段定义、验证规则、默认值等，
 * 为计划制定提供标准化支持。</p>
 *
 * @author 系统生成
 * @version 2.0
 * @since 2025-11-09
 */
@Entity
@Table(name = "exam_plan_template")
@EntityListeners(AuditingEntityListener.class)
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class ExamPlanTemplate {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 模板编码
     */
    @Column(name = "template_code", unique = true, length = 50, nullable = false)
    private String templateCode;

    /**
     * 模板名称
     */
    @Column(name = "template_name", length = 100, nullable = false)
    private String templateName;

    /**
     * 模板类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "template_type", length = 20, nullable = false)
    private TemplateType templateType;

    /**
     * 适用学年
     */
    @Column(name = "academic_year", length = 10)
    private String academicYear;

    /**
     * 适用学期
     */
    @Column(name = "semester", length = 20)
    private String semester;

    /**
     * 考试类型
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "exam_type", length = 20, nullable = false)
    private ExamType examType;

    /**
     * 模板内容（JSON格式）
     */
    @Column(name = "template_content", columnDefinition = "TEXT", nullable = false)
    private String templateContent;

    /**
     * 版本号
     */
    @Column(name = "version", length = 10, nullable = false)
    private String version;

    /**
     * 模板状态
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "status", length = 20, nullable = false)
    private TemplateStatus status;

    /**
     * 创建者ID
     */
    @Column(name = "creator_id", nullable = false)
    private Long creatorId;

    /**
     * 创建者姓名
     */
    @Column(name = "creator_name", length = 100)
    private String creatorName;

    /**
     * 模板描述
     */
    @Column(name = "template_description", length = 1000)
    private String templateDescription;

    /**
     * 验证规则（JSON格式）
     */
    @Column(name = "validation_rules", columnDefinition = "TEXT")
    private String validationRules;

    /**
     * 默认值（JSON格式）
     */
    @Column(name = "default_values", columnDefinition = "TEXT")
    private String defaultValues;

    /**
     * 适用院系
     */
    @Column(name = "applicable_department", length = 200)
    private String applicableDepartment;

    /**
     * 适用专业
     */
    @Column(name = "applicable_major", length = 200)
    private String applicableMajor;

    /**
     * 适用年级
     */
    @Column(name = "applicable_grade", length = 100)
    private String applicableGrade;

    /**
     * 是否系统模板
     */
    @Column(name = "is_system_template", nullable = false)
    private Boolean isSystemTemplate;

    /**
     * 是否默认模板
     */
    @Column(name = "is_default", nullable = false)
    private Boolean isDefault;

    /**
     * 是否启用
     */
    @Column(name = "is_enabled", nullable = false)
    private Boolean isEnabled;

    /**
     * 使用次数
     */
    @Column(name = "usage_count")
    private Integer usageCount;

    /**
     * 最后使用时间
     */
    @Column(name = "last_used_time")
    private LocalDateTime lastUsedTime;

    /**
     * 审核人ID
     */
    @Column(name = "approved_by")
    private Long approvedBy;

    /**
     * 审核人姓名
     */
    @Column(name = "approved_by_name", length = 100)
    private String approvedByName;

    /**
     * 审核时间
     */
    @Column(name = "approved_time")
    private LocalDateTime approvedTime;

    /**
     * 审核意见
     */
    @Column(name = "approval_comment", length = 1000)
    private String approvalComment;

    /**
     * 删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted;

    /**
     * 创建时间
     */
    @CreatedDate
    @Column(name = "create_time", nullable = false, updatable = false)
    private LocalDateTime createTime;

    /**
     * 更新时间
     */
    @LastModifiedDate
    @Column(name = "update_time", nullable = false)
    private LocalDateTime updateTime;

    /**
     * 生效时间
     */
    @Column(name = "effective_time")
    private LocalDateTime effectiveTime;

    /**
     * 失效时间
     */
    @Column(name = "expiry_time")
    private LocalDateTime expiryTime;

    /**
     * 模板字段定义列表
     */
    @OneToMany(mappedBy = "template", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private List<TemplateFieldDefinition> fieldDefinitions;

}
